Java Cookbook, 4th Edition by Ian F. Darwin
Author:Ian F. Darwin [Ian F. Darwin]
Language: eng
Format: epub
Publisher: O'Reilly Media, Inc.
Published: 2020-04-24T16:00:00+00:00
Figure 3-1. String, StringBuilder, and C-language strings
Although we haven’t discussed the details of the java.io package yet (we will, in [Link to Come]), you need to be able to read text files for some of these programs. Even if you’re not familiar with java.io, you can probably see from the examples of reading text files that a BufferedReader allows you to read “chunks” of data, and that this class has a very convenient readLine() method.
Going the other way, System.out.println() is normally used to print strings or other values to the terminal or “standard output.” String concatenation is commonly used here, as in:
System.out.println("The answer is " + result);
One caveat with string concatenation is that if you are appending a bunch of things, and a number and a character are concatenated at the front, they are added before concatenation due to Java’s precedence rules. So don’t do as I did in this contrived example:
int result = ...; System.out.println(result + '=' + " the answer.");
Given that result is an integer, then result + '=' (result added to the equals sign, which is of the numeric type char) is a valid numeric expression, which will result in a single value of type int. If the variable result has the value 42, and given that the character = in a Unicode (or ASCII) code chart has the value 61, this prints:
103 the answer.
The wrong value and no equals sign! Safer approaches include using parentheses, using double quotes around the equals sign, using a StringBuilder (see Recipe 3.2) or a MessageFormat (see [Link to Come]), or using String.format() (see [Link to Come]). Of course in this simple example you could just move the = to be part of the string literal, but the example was chosen to illustrate the problem of arithmetic on char values being confused with string contatenation. I won’t show you how to sort an array of strings here; the more general notion of sorting a collection of objects will be taken up in [Link to Come].
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26814)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(20680)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20455)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20452)
The Fifty Shades Trilogy & Grey by E L James(19326)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19273)
Shot Through the Heart by Mercy Celeste(19136)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17303)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17238)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(17085)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(17006)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16623)
The Subtle Art of Not Giving a F*ck by Mark Manson(14629)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14318)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13985)
Scorched Earth by Nick Kyme(12972)
Drei Generationen auf dem Jakobsweg by Stein Pia(11157)
Suna by Ziefle Pia(11083)
Scythe by Neal Shusterman(10562)